<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Setting extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        if ( !$this->permission->chkPermission( 'setting' ) )
        {
            $url = config_item( 'base_url' );
            redirect( $url );
        }
    }

    /**
     * 我的设置
     */
    public function index()
    {
        $this->email();
    }

    public function email()
    {
        $data = array( );
        $this->get_common_section( $data );
        $data['type'] = 'email';

        $userData = $this->model_user_getByUserId( $this->session->getUserSession( 'id' ) );
        $data['userData'] = $userData;

        $this->load->view( 'setting/email', $data );
    }

    public function editemail()
    {
        $data = array( );
        if ( $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
        {
            $emailData = $this->_getEmailData();
            if ( $emailData )
            {
                $this->model_user_updateUserEmailByUserId( $this->session->getUserSession( 'id' ), $emailData );
                //提示
                $messageData = array(
                    'title' => '操作成功',
                    'message' => "邮箱已修改，4秒后自动跳转回设置首页",
                    'autoRedirect' => true,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => my_url( 'setting', '', '', false ),
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data['message'] = $messageBox;

                $this->get_common_section( $data );
                $this->load->view( 'setting/email', $data );
            }
            else
            {
                $this->email();
            }
        }
        else
        {
            $this->email();
        }
    }

    public function editpassword()
    {
        $data = array( );
        $data['type'] = 'password';
        if ( $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
        {
            $passwordData = $this->_getPasswordData();
            if ( $passwordData )
            {
                $userData = $this->model_user_valid( $this->session->getUserSession( 'username' ), md5( $passwordData['passwordnow'] ) );
                if ( empty( $userData ) )
                {
                    $data['extraTip'] = '<p>原密码有误</p>';
                }
                else
                {
                    $this->model_user_updateUserPasswordByUserId( $this->session->getUserSession( 'id' ), md5( $passwordData['password'] ) );
                    //提示
                    $messageData = array(
                        'title' => '操作成功',
                        'message' => "密码已修改，4秒后自动跳转回设置首页",
                        'autoRedirect' => true,
                        'autoRedirectTimeOut' => 4,
                        'autoRedirectURL' => my_url( 'setting', '', '', false ),
                    );
                    $messageBox = Modules::run( 'messageBox', $messageData );
                    $data['message'] = $messageBox;
                }
                $this->get_common_section( $data );
                $this->load->view( 'setting/password', $data );
            }
            else
            {
                $this->password();
            }
        }
        else
        {
            $this->password();
        }
    }

    public function password()
    {
        $data = array( );
        $this->get_common_section( $data );
        $data['type'] = 'password';

        $this->load->view( 'setting/password', $data );
    }

    /**
     * 获取邮箱数据
     */
    private function _getEmailData()
    {
        $this->form_validation->set_message( 'required', '%s不能为空' );
        $this->form_validation->set_message( 'valid_email', '%s格式不合法' );
        $this->form_validation->set_rules( 'email', '邮箱', 'required|valid_email' );
        if ( $this->form_validation->run() ) return $this->input->post( 'email' );
        return false;
    }

    private function _getPasswordData()
    {
        $this->form_validation->set_message( 'required', '%s不能为空' );
        $this->form_validation->set_message( 'matches', '%s不正确' );
        $this->form_validation->set_message( 'min_length', '%s长度至少为6位' );
        $this->form_validation->set_rules( 'passwordnow', '原密码', 'required' );
        $this->form_validation->set_rules( 'password', '新密码', 'required|min_length[6]' );
        $this->form_validation->set_rules( 'passwordconfirm', '确认密码', 'required|matches[password]' );
        if ( $this->form_validation->run() )
        {
            $data = array(
                'passwordnow' => $this->input->post( 'passwordnow' ),
                'password' => $this->input->post( 'password' ),
            );
            return $data;
        }
        return false;
    }

}